#include "sample_drv_adapter.h"
#define LOG_TAG "SAMPLE_DRV_ADAPTER"
#include "elog.h"

// 全局变量：采集驱动适配层接口实例
sample_drv_adapter_api_t g_sample_temp_drv_adapter_api;
sample_drv_adapter_api_t g_sample_bright_drv_adapter_api;

// 全局变量：任务句柄（初始化NULL）
TaskHandle_t sample_temp_task_handler = NULL;
TaskHandle_t sample_bright_task_handler = NULL;

// 任务函数声明（简化名称）
void sample_temp_task_process_entry(void* p);
void sample_bright_task_entry(void* p);

// 全局变量：驱动初始化状态
static volatile bool temp_drv_status = false;
static volatile bool bright_drv_status = false;

/**
 * @desc  : 采集驱动适配层初始化
 * @date  : 2025-11-11
 * @author: AnRui
 * @note  : 绑定驱动函数指针、初始化传感器、创建采集任务
 */
void sample_drv_adapter_init(void)
{
    log_d("sample drv adapter init start");

    // 绑定温度传感器驱动函数（NTC）
    g_sample_temp_drv_adapter_api.p_sensor_init = ntc_drv_init;
    g_sample_temp_drv_adapter_api.p_sensor_sample_value = ntc_trigger_and_get_temp;

    // 绑定亮度传感器驱动函数（AP3216C）
    g_sample_bright_drv_adapter_api.p_sensor_init = ap3216c_init;
    g_sample_bright_drv_adapter_api.p_sensor_sample_value = ap3216c_get_als;

    // 初始化温度传感器
    if (g_sample_temp_drv_adapter_api.p_sensor_init()) {
        log_d("sample temp drv init success");
        temp_drv_status = true;
    } else {
        log_e("sample temp drv init fail");
        temp_drv_status = false;
    }

    // 初始化亮度传感器
    if (g_sample_bright_drv_adapter_api.p_sensor_init()) {
        log_d("sample bright drv init success");
        bright_drv_status = true;
    } else {
        log_e("sample bright drv init fail");
        bright_drv_status = false;
    }

    // 创建温度采集适配层任务
    if (xTaskCreate(sample_temp_task_process_entry,
        "sample_temp_adapter_task",
        ADA_SWITCH_FAN_TASK_STACK_SIZE,
        NULL,
        ADA_SWITCH_FAN_TASK_PRIORITY,
        &sample_temp_task_handler) != pdTRUE) {
        log_e("create sample temp adapter task failed");
    }

    // 创建亮度采集适配层任务
    if (xTaskCreate(sample_bright_task_entry,
        "sample_bright_adapter_task",
        ADA_SWITCH_LIGHT_TASK_STACK_SIZE,
        NULL,
        ADA_SWITCH_LIGHT_TASK_PRIORITY,
        &sample_bright_task_handler) != pdTRUE) {
        log_e("create sample bright adapter task failed");
    }

    log_d("sample drv adapter init end");
}

/**
 * @desc  : 温度采集适配层任务（响应中间层采集请求）
 * @date  : 2025-11-11
 * @author: AnRui
 * @param : p - 任务参数（未使用）
 * @note  : 接收中间层请求，调用NTC驱动采集温度，回写数据到共享内存
 */
void sample_temp_task_process_entry(void* p)
{
    midlayer_sample_temperature_t* p_mid_data = (midlayer_sample_temperature_t*)midlayer_sample_tempraure_mmc_ctrl.p_shared_mem;
    int tmp = 0;
    while (1) {
        // 等待来自中间层的采集请求
        xSemaphoreTake(midlayer_sample_tempraure_mmc_ctrl.sem_req, portMAX_DELAY);

        // 驱动初始化失败，标记数据无效
        if (!temp_drv_status) {
            p_mid_data->is_valid = false;
            log_e("temp drv not initialized");
            // 回复中间层：采集完成
            xSemaphoreGive(midlayer_sample_tempraure_mmc_ctrl.sem_ack);
            continue;
        }

        // 检测温度值是否合法
        tmp = g_sample_temp_drv_adapter_api.p_sensor_sample_value();
        if (-1 == tmp) {
            p_mid_data->is_valid = false;
            log_d("sample temp value %d , is wrong", tmp);
        } else {
            p_mid_data->temperature = tmp;
            p_mid_data->is_valid = true;
            log_d("sample temp success: %d", p_mid_data->temperature);
        }
        xSemaphoreGive(midlayer_sample_tempraure_mmc_ctrl.sem_ack);
    }
}

/**
 * @desc  : 亮度采集适配层任务（响应中间层采集请求）
 * @date  : 2025-11-11
 * @author: AnRui
 * @param : p - 任务参数（未使用）
 * @note  : 接收中间层请求，调用AP3216C驱动采集亮度，回写数据到共享内存
 */
void sample_bright_task_entry(void* p)
{
    midlayer_sample_bright_t* p_mid_data = (midlayer_sample_bright_t*)midlayer_sample_bright_mmc_ctrl.p_shared_mem;
    int tmp = 0;
    while (1) {
        // 等待来自中间层的采集请求
        xSemaphoreTake(midlayer_sample_bright_mmc_ctrl.sem_req, portMAX_DELAY);

        // 驱动初始化失败，标记数据无效
        if (!bright_drv_status) {
            p_mid_data->is_valid = false;
            log_w("bright drv not initialized");
            // 回复中间层：采集完成
            xSemaphoreGive(midlayer_sample_bright_mmc_ctrl.sem_ack);
            continue;
        }
        tmp = g_sample_bright_drv_adapter_api.p_sensor_sample_value();

        // 检测亮度值是否合法
        if (tmp < 0) {
            p_mid_data->is_valid = false;
            log_d("sample bright value %d , is wrong", tmp);
        } else {
            // 调用驱动采集亮度，回写数据到中间层共享内存
            p_mid_data->bright = tmp;
            p_mid_data->is_valid = true;
            log_d("sample bright success: %d", p_mid_data->bright);
        }
        // 回复中间层：采集完成
        xSemaphoreGive(midlayer_sample_bright_mmc_ctrl.sem_ack);
    }
}
